<!-- 管理员页面 -->
<template>
  <div style="height: 6vh; background-image: linear-gradient(to right, #fffacd, #add8e6); padding: 10px 20px">
    <span style="line-height: 60px;  font-size: 20px;font-weight: bold">管理员界面</span>
    <el-button type="primary" style="float: right; margin-top: 10px" @click="beforeAddAdmin">新增管理员</el-button>
  </div>
  <!-- 保存管理员数据弹窗 -->
  <el-dialog :title="dialogTitle" style="width: 1000px; padding: 40px;" v-model="dialogVisible"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="管理员名">
            <el-input placeholder="请输入管理员名" v-model="saveAdminForm.adminname"
                      :disabled="saveAdminForm.id!=null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input placeholder="请输入手机号" v-model="saveAdminForm.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input placeholder="请输入邮箱" v-model="saveAdminForm.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input placeholder="请输入年龄" type="number" v-model="saveAdminForm.age"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="职级">
            <el-select placeholder="请输入" v-model="saveAdminForm.level" @change="loadLeader">
              <el-option label="员工" value="10"></el-option>
              <el-option label="经理" value="20"></el-option>
              <el-option label="总监" value="30"></el-option>
              <el-option label="总裁" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="直属领导">
            <el-select placeholder="请输入" v-model="saveAdminForm.parentId">
              <el-option v-for="item in leaderOptions" :label="item.adminname" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="性别">
            <el-radio-group v-model="saveAdminForm.gender">
              <el-radio border label="男" value="1" style="margin: 0"></el-radio>
              <el-radio border label="女" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="管理员状态">
            <el-radio-group v-model="saveAdminForm.status">
              <el-radio border label="启用" value="1" style="margin: 0"></el-radio>
              <el-radio border label="禁用" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveAdmin">保存</el-button>
    </template>
  </el-dialog>

  <!-- 管理员搜索卡片 -->
  <el-card style="margin: 20px; height: 70px;">
    <!-- :inline="true" 是告诉 el-form 组件应该将它的子元素（即表单项）排列在同一行，而不是默认的块级排列 -->
    <el-form :inline="true">
      <el-form-item label="管理员搜索" style="width: 270px;">
        <el-input placeholder="请输入管理员名" v-model="searchAdminForm.adminname" @keydown.enter="loadAdmin"></el-input>
      </el-form-item>
      <el-form-item label="管理员状态" style="width: 290px;">
        <el-select placeholder="请选择管理员状态" v-model="searchAdminForm.status" @keydown.enter="loadAdmin" @change="loadAdmin">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadAdmin">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 管理员数据表格卡片 -->
  <el-card style="margin: 20px; ">
    <el-table :data="adminArr">
      <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="管理员" prop="adminname" align="center"></el-table-column>
      <el-table-column label="手机号" prop="phone" align="center"></el-table-column>
      <el-table-column label="加入时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template #default="scope">
          <el-switch active-value="1" inactive-value="0"
                     v-model="scope.row.status"
                     :disabled="scope.row.level == 40"
                     @change="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link :disabled="scope.row.level==40" @click="resetPassword(scope.row.id)">密码重置
          </el-button>
          <el-button type="primary" link :disabled="scope.row.level==40" @click="editAdmin(scope.row.id)">编辑
          </el-button>
          <el-button type="primary" link :disabled="scope.row.level==40" @click="deleteAdmin(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

//初始化 一进到页面立即加载所有管理员的数据
onMounted(() => {
  loadAdmin()
})

//变量定义
const dialogVisible = ref(false) // 控制弹窗是否出现
const leaderOptions = ref([]) // 维护可选直属领导的领导数组
const saveAdminForm = ref({ // 保存管理员表单数据
  adminname: '',
  phone: '',
  email: '',
  age: '',
  level: '',
  parentId: '',
  gender: '',
  status: ''
})
const searchAdminForm = ref({adminname: '', status: ''}) // 保存管理员搜索条件
const adminArr = ref([]) // 保存管理员表格数据
const dialogTitle = ref('新增管理员') // 维护弹窗标题

//数据获取
// 加载管理员数据
const loadAdmin = () => {
  let data = qs.stringify(searchAdminForm.value)
  axios.get(BASE_URL + '/p1/admin/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      adminArr.value = response.data.data
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}
// 定义加载可选领导数据的方法
const loadLeader = () => {
  saveAdminForm.value.parentId = ''
  leaderOptions.value = []
  let level = parseInt(saveAdminForm.value.level) + 10
  axios.get(BASE_URL + '/p1/admin/select?level=' + level).then((response) => {
    if (response.data.code == 2000) {
      leaderOptions.value = response.data.data
    } else
      ElMessage.error(response.data.msg)
  })
}

//管理员操作
// 每次点击“新增管理员”按钮时调用的方法
const beforeAddAdmin = () => {
  dialogTitle.value = '新增管理员'
  dialogVisible.value = true
}
// 保存管理员
const saveAdmin = () => {
  let data = qs.stringify(saveAdminForm.value)
  axios.post(BASE_URL + '/p1/admin/save', data).then((Response) => {
    if (Response.data.code == 2000) {
      ElMessage.success('保存成功！')
      dialogVisible.value = false
      saveAdminForm.value = {}
      loadAdmin() // 新增成功后，需要重新加载数据
    } else {
      ElMessage.error('保存失败！')
    }
  })
}
// 定义编辑管理员的方法
const editAdmin = (id) => {
  dialogTitle.value = '编辑管理员'
  dialogVisible.value = true
  axios.get(BASE_URL + '/p1/admin/select?id=' + id).then((response) => {
    if (response.data.code == 2000) {
      saveAdminForm.value = response.data.data[0]
      loadLeader()
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}
// 定义重置密码的方法 restful风格
const resetPassword = (adminId) => {
  axios.post(BASE_URL + '/p1/admin/update/password/' + adminId).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('密码重置成功！')
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}
// 定义修改员工状态的方法
const changeStatus = (adminId, status) => {
  axios.post(BASE_URL + '/p1/admin/update/status/' + adminId + '/' + status).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('修改状态成功！')
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}
// 定义删除管理员的方法
const deleteAdmin = (adminId) => {
  if (confirm('您确定要删除吗？')) {
    axios.delete(BASE_URL + '/p1/admin/delete/' + adminId).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功！')
        loadAdmin() // 删除管理员之后重新加载数据
      } else {
        ElMessage.error(response.data.msg)
      }
    })
  }
}

//辅助功能
// 定义处理弹窗关闭的方法
const handleClose = () => {
  if (confirm('您是否要关闭此弹窗？')) {
    saveAdminForm.value = {}
    dialogVisible.value = false
  }
}
// 重置搜索
const resetSearch = () => {
  searchAdminForm.value = {}
  loadAdmin()
}
</script>

<style scoped>

</style>